package com.beamfield.controller.manage.process;

import static java.util.Comparator.naturalOrder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.PersonInfo;
import com.beamfield.model.ProcDoc;
import com.beamfield.model.ProcJlzz;
import com.beamfield.model.ProcNotice;
import com.beamfield.model.ProcYhsg;
import com.beamfield.model.Process;
import com.beamfield.model.SavebeamDado;
import com.beamfield.model.TechDoc;
import com.beamfield.modelVo.MaterialStraceVo;
import com.beamfield.modelVo.ProcNoticeVo;
import com.beamfield.modelVo.ProcNoticeWithDetailVo;
import com.beamfield.modelVo.TechDocVo;
import com.beamfield.modelVo.TreeVo;
import com.beamfield.service.manage.process.ProcessService;

/**
 * 制梁工序
 */
@Controller
@RequestMapping("/cms/process/project")
public class ProcessController extends BaseController{
    private final ProcessService processService;
    @Autowired
    public ProcessController(ProcessService processService) {
        this.processService = processService;
    }
    @RequestMapping(value = "/newProject",method = RequestMethod.GET)
    public ModelAndView newNotice(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/techdoc",method = RequestMethod.GET)
    public ModelAndView techdoc(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/process_list",method = RequestMethod.GET)
    public ModelAndView projectList(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/notice_detail",method = RequestMethod.GET)
    public ModelAndView noticeDetail(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/techdoc_view",method = RequestMethod.GET)
    public ModelAndView techdocView(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/gjbz",method = RequestMethod.GET)
    public ModelAndView gjbz(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/mbaz",method = RequestMethod.GET)
    public ModelAndView mbaz(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/hnt",method = RequestMethod.GET)
    public ModelAndView hnt(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/cmsg",method = RequestMethod.GET)
    public ModelAndView cmsg(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/yzl",method = RequestMethod.GET)
    public ModelAndView yzl(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/czl",method = RequestMethod.GET)
    public ModelAndView czl(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/zzl",method = RequestMethod.GET)
    public ModelAndView zzl(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/ycl",method = RequestMethod.GET)
    public ModelAndView ycl(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/qsyj",method = RequestMethod.GET)
    public ModelAndView qsyj(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/fm",method = RequestMethod.GET)
    public ModelAndView fm(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/fs",method = RequestMethod.GET)
    public ModelAndView fs(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/jl",method = RequestMethod.GET)
    public ModelAndView jl(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/yhsg",method = RequestMethod.GET)
    public ModelAndView yhsg(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/addyhsg",method = RequestMethod.GET)
    public ModelAndView addyhsg(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/details/updateYhsg",method = RequestMethod.GET)
    public ModelAndView updateYhsg(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/production",method = RequestMethod.GET)
    public ModelAndView production(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/process_detail",method = RequestMethod.GET)
    public ModelAndView processDtail(){
        return new ModelAndView();
    }

    /**
     * 根据id获取存梁台座
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOneSaveBeamDado")
    @ResponseBody
    public ApiResult getOneSaveBeamDado(HttpServletRequest request){
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            return new ApiResult();
        }
        SavebeamDado oneSaveBeamDado = processService.getOneSaveBeamDado(id);
        return new ApiResult(oneSaveBeamDado);
    }
    /**
     * 获取存梁台座
     * @param request
     * @return
     */
    @RequestMapping(value = "getSaveBeamDado")
    @ResponseBody
    public ApiResult getSaveBeamDado(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        //区域
        String area = request.getParameter("area");
        //台座
        String dado = request.getParameter("dado");
        //上下位置
        String location = request.getParameter("location");
        try {
            return processService.getSaveBeamDado(beamId, area, dado, location);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取未生产的施工部位
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProjectDetail",method = RequestMethod.GET)
    public ApiResult getProjectDetail(HttpServletRequest request){
        String projectId = request.getParameter("projectId");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(projectId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), "请先选择工程");
        }
        return processService.getProjectDetail(projectId, beamId);
    }
    /**
     * 保存通知单
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveProcNotice",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveProcNotice(HttpServletRequest request, String procNoticeJson){
        if(StringUtils.isEmpty(procNoticeJson)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{
            ProcNotice procNotice = JSON.parseObject(procNoticeJson, ProcNotice.class);
            String depart = procNotice.getDepart();
            if(StringUtils.isEmpty(depart)){
                return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),"请选择通知的部门");
            }
            Long beamId = super.getBeamId(request);
            Long userId = super.getUserId(request);
            procNotice.setBeam_id(beamId);
            procNotice.setOperator(userId);
            procNotice.setAddtime(new Date());
            result = processService.saveProcNotice(procNotice);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"保存成功");
        }else {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }


    /**
     * 保存技术交底书
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveTechDoc",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveTechDoc(HttpServletRequest request,String techDocJson){
        if(StringUtils.isEmpty(techDocJson)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        String result = null;
        TechDoc techDoc = JSON.parseObject(techDocJson, TechDoc.class);
        try{
            Long beamId = super.getBeamId(request);
            Long userId = super.getUserId(request);
            techDoc.setBeam_id(beamId);
            techDoc.setOperator(userId);
            techDoc.setAddtime(new Date());
            result = processService.saveTechDoc(techDoc);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(StringUtils.isNotEmpty(result)){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"保存成功",result);
        }else {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取技术交底书
     * @return
     */
    @RequestMapping(value = "/getTechDocByNoticeId", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getTechDocByNoticeId(String noticeId){
        if(StringUtils.isEmpty(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        return processService.getTechDocByNoticeId(noticeId);
    }
    /**
     * 获取制梁通知单
     * @param request
     * @return
     */
    @RequestMapping(value = "/readProcessData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readProcessData(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String noticeId = request.getParameter("notice_id");
        Long beamId = super.getBeamId(request);
        String projectName = request.getParameter("projectName");
        String date = request.getParameter("date");
        String step = request.getParameter("step");
        List<ProcNoticeVo> processDataList = processService.getProcessData(offset, limit, beamId, projectName, date, noticeId, step);
        long total = processService.countProcessData(beamId, projectName, date, noticeId, step);
        return new PageResult(total, CommonUtils.parseLong(offset,0L), processDataList);
    }

    /**
     * 获取制梁通知单和工序通知单
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProcNoticeWithDetail",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getProcNoticeWithDetail(HttpServletRequest request){
        String noticeId = request.getParameter("notice_id");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        ProcNoticeWithDetailVo procNoticeWithDetailVo = null;
        try{
            procNoticeWithDetailVo = processService.getProcNoticeWithDetail(noticeId,beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        return new ApiResult(procNoticeWithDetailVo);
    }
    /**
     * 获取技术交底书
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTechdocData",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getTechdocData(HttpServletRequest request){
        String jsjdsNo = request.getParameter("jsjds_no");
        if(StringUtils.isEmpty(jsjdsNo)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        TechDocVo techDocVo = null;
        try{
            techDocVo = processService.getTechdocData(jsjdsNo);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        return new ApiResult(techDocVo);
    }

    /**
     * 工序页面获取数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProcessStep",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getProcessStep(HttpServletRequest request){
        String noticeId = request.getParameter("notice_id");
        String step = request.getParameter("step");
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            switch (Integer.parseInt(step)) {
                //钢筋绑扎
                case 1 :return processService.getGJBZData(noticeId, userId, step);
                //模板安装
                case 2 :return processService.getMBAZData(noticeId, userId, step);
                //混凝土
                case 3 : return processService.getHNTData(noticeId, userId, step);
                //拆模施工
                case 4 : return processService.getCMSGData(noticeId, userId, step);
                //养护施工
                case 5 : return processService.getYHSGData(noticeId, userId, step, beamId);
                //预张拉
                case 6 : return processService.getYZLData(noticeId, userId, step);
                //初张拉
                case 7 : return processService.getCZLData(noticeId, userId, step);
                //移存梁
                case 8 : return processService.getYCLData(noticeId, userId, step);
                //终张拉
                case 9 : return processService.getZZLData(noticeId, userId, step);
                //切丝压浆
                case 10 : return processService.getQSYJData(noticeId, userId, step);
                //封锚
                case 11 : return processService.getFMData(noticeId, userId, step);
                //防水
                case 12 : return processService.getFSData(noticeId, userId, step);
                //架梁
                case 13 : return processService.getJLData(noticeId, userId, step);
                default : return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 审核历史
     * @param request
     * @return
     */
    @RequestMapping(value = "/getReviewHistory",method = RequestMethod.GET)
    @ResponseBody
    public PageResult getReviewHistory(HttpServletRequest request,Integer offset, Integer limit){
        String noticeId = request.getParameter("notice_id");
        String step = request.getParameter("step");
        Long beamId = super.getBeamId(request);
        return processService.getReviewHistory(noticeId, step, null, beamId);
    }
    /**
     * 工序页面获取数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProcessYhsgDetail",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getProcessYhsgDetail(HttpServletRequest request){
        String noticeId = request.getParameter("notice_id");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            return processService.getProcessYhsgDetail(noticeId, beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    @RequestMapping("/importYhsg")
    @ResponseBody
    public ApiResult importYhsg(HttpServletRequest request, @RequestParam("excelFile") MultipartFile multipartFile){
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        String noticeId = request.getParameter("notice_id");
        if (multipartFile == null) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            long fileId = super.saveFile((CommonsMultipartFile) multipartFile);
            InputStream in = multipartFile.getInputStream();
            byte[] data = IOUtils.toByteArray(in);
            Workbook wb = null;
            FormulaEvaluator formulaEvaluator;
            try {
                wb = new HSSFWorkbook(new ByteArrayInputStream(data));
                formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
            } catch (Exception e) {
                wb = new XSSFWorkbook(new ByteArrayInputStream(data));
                formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) wb);
            }
            return processService.importYhsg(noticeId, wb, fileId, userId, beamId, formulaEvaluator);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), e.getMessage());
        }
    }

    /**
     * 保存工序人员数据和工序台座班组数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveProcessStep",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveProcessStep(HttpServletRequest request,String ecodeJson,String notice_id,String step, String dado, String group,@RequestParam(value = "procJlzzJson", required = false) String procJlzzJson){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(ecodeJson)||StringUtils.isEmpty(notice_id)||StringUtils.isEmpty(step)||StringUtils.isEmpty(dado) ||StringUtils.isEmpty(group)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            List<String> ecodeList = JSON.parseArray(ecodeJson,String.class);
            if(ecodeList == null || ecodeList.isEmpty()){
                return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
            }
            if(StringUtils.isNotEmpty(procJlzzJson)) {
                ProcJlzz procJlzz = JSON.parseObject(procJlzzJson, ProcJlzz.class);
                if (procJlzz != null) {
                    procJlzz.setBeam_id(beamId);
                    procJlzz.setNotice_id(CommonUtils.parseLong(notice_id, 0L));
                    procJlzz.setOperator(userId);
                    processService.saveProcJlzz(procJlzz);
                }
            }
            return processService.saveProcessStep(ecodeList,notice_id, step, dado, beamId, userId, group);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 养护施工提交通过
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveProcessStep$YHSG",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveProcessStep$YHSG(HttpServletRequest request,String notice_id,String step){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(notice_id)||StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            return processService.saveProcessStep$YHSG(notice_id, step, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存通知单审核数据
     * @param request
     * @param jcx 检测项
     * @param pass 是否通过 1、0
     * @param remark 备注
     * @param type 类型 有两个检测项时区分
     * @return
     */
    @RequestMapping(value = "/saveProcessReview",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveProcessReview(HttpServletRequest request,String jcx, String pass, String remark, String type, String notice_id, String step){
        if(StringUtils.isEmpty(jcx)||StringUtils.isEmpty(pass)|| StringUtils.isEmpty(type)||StringUtils.isEmpty(notice_id)||StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        try {
            switch (Integer.parseInt(step)) {
                //钢筋绑扎
                case 1 : return processService.saveProcessReview$GJBZ(jcx, notice_id, pass, remark, beamId, userId, type);
                //模板安装
                case 2 : return processService.saveProcessReview$MBAZ(jcx, notice_id, pass, remark, beamId, userId);
                //混凝土
                case 3 : return processService.saveProcessReview$HNT(jcx, notice_id, pass, remark, beamId, userId);
                //预张拉
                case 6 : return processService.saveProcessReview$YZL(jcx, notice_id, pass, remark, beamId, userId);
                //初张拉
                case 7 : return processService.saveProcessReview$CZL(jcx, notice_id, pass, remark, beamId, userId);
                //终拉张
                case 9 : return processService.saveProcessReview$ZZL(jcx, notice_id, pass, remark, beamId, userId);
                //切丝压浆
                case 10 : return processService.saveProcessReview$QSYJ(jcx, notice_id, pass, remark, beamId, userId, type);
                //封锚
                case 11 : return processService.saveProcessReview$FM(jcx, notice_id, pass, remark, beamId, userId, type);
                //防水
                case 12 : return processService.saveProcessReview$FS(jcx, notice_id, pass, remark, beamId, userId, type);
                //架梁
                case 13 : return processService.saveProcessReview$JL(jcx, notice_id, pass, remark, beamId, userId, type);
                default :return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存养护施工数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveYhsgDetail")
    @ResponseBody
    public ApiResult saveYhsgDetail(HttpServletRequest request, String yhsgDetailJson, String notice_id){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if (StringUtils.isEmpty(yhsgDetailJson)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        try {
            List<ProcYhsg> yhsgDetailList = JSON.parseArray(yhsgDetailJson, ProcYhsg.class);
            if (yhsgDetailList == null || yhsgDetailList.isEmpty()) {
                return apiResult;
            }
            for (int i = 0; i <yhsgDetailList.size() ; i++) {
                apiResult=processService.saveYhshDetail(beamId, userId, yhsgDetailList.get(i), notice_id);
            }
            return apiResult;

        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }


    }

    /**
     * 保存工序记录表
     * @param procDoc
     * @param request
     * @return
     */
    @RequestMapping("/saveProcDoc")
    @ResponseBody
    public ApiResult saveProcDoc(ProcDoc procDoc,HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        ApiResult apiResult = processService.saveProcessDoc(procDoc, beamId, userId);
        return apiResult;
    }
    @RequestMapping("/delProDoc")
    @ResponseBody
    public ApiResult delProcDoc(Long id,HttpServletRequest request){
        Long userId = super.getUserId(request);
        processService.delProcessDoc(id,userId);
        return new ApiResult();
    }

    /**
     * 获取记录表列表
     */
    @RequestMapping("/getProDocList")
    @ResponseBody
    public PageResult getProDocList(Long notice_id,Long step,HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        List<ProcDoc> proDocList = processService.getProDocList(notice_id, step, beamId);
        return new PageResult(proDocList.size(),1,proDocList);
    }

    /**
     * 进入指定工序前判断是否存在此道工序的通知单，
     * 如果通知单不存则不能进入这道工序，需要先新增通知单。
     * @param request
     * @return
     */
    @RequestMapping(value = "/decideCanEnterThisProc")
    @ResponseBody
    public ApiResult decideCanEnterThisProc(HttpServletRequest request){
        String noticeId = request.getParameter("noticeId");
        Long beamId = super.getBeamId(request);
        String step = request.getParameter("step");
        if(StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            return processService.decideCanEnterThisProc(beamId, noticeId, step);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    @RequestMapping(value = "/getSearchDate")
    @ResponseBody
    public ApiResult getSearchDate(HttpServletRequest request, String start_date, String end_date){
    	Long beamId = super.getBeamId(request);
    	String beamName = super.getBeamName(request);
    	try {
    		return processService.getSearchDate(beamId,beamName, start_date, end_date);
    	}catch (Exception e){
    		e.printStackTrace();
    		return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
    	}
    }

    @RequestMapping(value = "/getSearchNo")
    @ResponseBody
    public ApiResult getSearchNo(HttpServletRequest request, String searchNo){
        Long beamId = super.getBeamId(request);
        String beamName = super.getBeamName(request);
        try {
            return processService.getSearchNo(beamId,beamName, searchNo);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }


    /**
     * 生产管理首页获取统计数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/getStatistics")
    @ResponseBody
    public ApiResult getStatistics(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String beamName = super.getBeamName(request);
        try {
            return processService.getStatistics(beamId,beamName);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 生产管理首页获取架梁位置
     * @param request
     * @return
     */
    @RequestMapping(value = "/readFrameBeamData")
    @ResponseBody
    public PageResult readFrameBeamData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
//        String limit = "10";
        try {
            return processService.readFrameBeamData(beamId, offset, limit);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/getNoticeId")
    @ResponseBody
    public ApiResult getNoiticeId(HttpServletRequest request,String project_detail_id,String project_id){
        Long beamId = super.getBeamId(request);
        ApiResult apiresult = processService.getNoticeIdByBeam_NoAndProject_id(project_detail_id, project_id, beamId);
        return  apiresult;
    }

    @RequestMapping(value = "/getProcNoticeByProject_detail_id",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getProcNoticeByProject_detail_id(HttpServletRequest request,String project_detail_id,String project_id){
        Long beamId = super.getBeamId(request);
        ApiResult apiresult = processService.getNoticeIdByBeam_NoAndProject_id(project_detail_id, project_id, beamId);
        String noticeId="";
        if (apiresult.getResult()!=null){
            noticeId = apiresult.getResult().toString();
        }
        if (StringUtils.isEmpty(noticeId)){
            return apiresult;
        }
        ProcNoticeWithDetailVo procNoticeWithDetailVo = null;
        try{
            procNoticeWithDetailVo = processService.getProcNoticeWithDetail(noticeId,beamId);
            procNoticeWithDetailVo.setNoticeid(noticeId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        return new ApiResult(procNoticeWithDetailVo);
    }
    /**
     * 获取物资追溯
     * @param request
     * @return
     */
    @RequestMapping(value = "/readTraceData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult readTraceData(HttpServletRequest request){
        //梁id
        String projectDetailId = request.getParameter("projectDetailId");
        if(StringUtils.isEmpty(projectDetailId)){
            return null;
        }
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        List<MaterialStraceVo> materialStraceVoList = processService.getSuppliesTrace(projectDetailId, offset, limit);
        long total = processService.countSuppliesTrace(projectDetailId);
        return new PageResult(total, CommonUtils.parseLong(offset, 0L), materialStraceVoList);
    }


    @RequestMapping(value = "/upnotice",method = RequestMethod.GET)
    public ModelAndView upnotice(){
        return new ModelAndView();
    }

    /**
     * 根据台座或者通知单id获取当前通知单所在工序
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProceseByDadoAndNoticeId", method = RequestMethod.GET)
    public ApiResult getProceseByDadoAndNoticeId(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String noticeId = request.getParameter("noticeId");
        String dado = request.getParameter("dado");
        Process pr = processService.getProceseByDadoAndNoticeId(dado, beamId, noticeId);
        return new ApiResult(pr);
    }
    /**
     * 获取所有台座
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllDado", method = RequestMethod.GET)
    public ApiResult getAllDado(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        List<TreeVo> treeVoList = processService.getAllDado(beamId);
        return new ApiResult(treeVoList);
    }

    /**
     * 获取工序不是14和未生产的梁
     * @param request
     * @return
     */
    @RequestMapping(value = "/getAllBeamNo", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getAllBeamNo(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        return processService.getAllBeamNo(beamId);
    }

    /**
     * 获取工序人员打卡信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOperatorPunch", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getOperatorPunch(HttpServletRequest request){
        String ecode = request.getParameter("ecode");
        String noticeId = request.getParameter("noticeId");
        String step = request.getParameter("step");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(ecode) || StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            List<PersonInfo> personInfoList = processService.getOperatorPunch(ecode, noticeId, step, beamId);
            Set<String> punchDate = personInfoList.stream()
                    .map(s -> DateFormatUtils.format(s.getSigin_time(), "yyyy-MM-dd")).collect(Collectors.toSet());
//            Date startDate = stream.max(Comparator.naturalOrder()).orElse(null);
//            Date lastDate = stream.min(Comparator.naturalOrder()).orElse(null);
            return new ApiResult(punchDate);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取工序人员打卡信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOperatorPunchTime", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getOperatorPunchTime(HttpServletRequest request){
        String ecode = request.getParameter("ecode");
        String noticeId = request.getParameter("noticeId");
        String step = request.getParameter("step");
        String queryDateStr = request.getParameter("queryDate");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(ecode) || StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step) || StringUtils.isEmpty(queryDateStr)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            Date queryDate = DateUtils.parseDate(queryDateStr, "yyyy-MM-dd");
            List<PersonInfo> personInfoList = processService.getOperatorPunch(ecode, noticeId, step, beamId);
            Set<String> punchTimeSet = personInfoList.stream().map(PersonInfo::getSigin_time)
                    .filter(s -> DateUtils.isSameDay(queryDate, s))
                    .sorted(naturalOrder())
                    .map(s -> DateFormatUtils.format(s, "HH:ss:mm")).collect(Collectors.toSet());
            return new ApiResult(punchTimeSet);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取各道工序审核人员
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProcessReviewer", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getProcessReviewer(HttpServletRequest request){
        String noticeId = request.getParameter("noticeId");
        String step = request.getParameter("step");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(noticeId) || StringUtils.isEmpty(step)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return processService.getProcessReviewer(noticeId,Integer.parseInt(step),beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
}
